package com.digua.base.util

import com.alibaba.fastjson2.JSON
import com.blankj.utilcode.util.StringUtils
import java.math.BigDecimal
import java.util.Locale

object StrUtil {
    val HTTP = "http"
    val HTTPS = "https"
    val COM = ".com"
    val separator = "/"
    val separatorDob = "//"

    /**
     * 传入数据是否是基本数据类型
     */
    fun isBaseData(data: Any?): Boolean {
        if (data == null) return false
        return when (data) {
            is String -> true
            is Int -> true
            is Boolean -> true
            is Double -> true
            is BigDecimal -> true
            is Float -> true
            is Long -> true
            is Char -> true
            is Byte -> true
            is Short -> true
            else -> false
        }
    }

    fun toStr(data: Any?): String? {
        if (data == null) return null
        if (isBaseData(data)) return data.toString()
        try {
            return JSON.toJSONString(data)
        } catch (e: Exception) {
            return data.toString()
        }
    }

    fun isBlank(str: CharSequence?): Boolean {
        return StringUtils.isEmpty(str) || str?.trim()?.length == 0
    }

    fun isBlankOrNull(str: CharSequence?): Boolean {
        return StringUtils.isEmpty(str) || str?.trim()?.length == 0 || str.toString().uppercase(Locale.getDefault()) == "NULL"
    }

    fun isNotBlank(str: CharSequence?): Boolean {
        return !isBlank(str)
    }

    /**
     * 判断是否全部为空字符串
     *
     * @param str
     * @return
     */
    fun allBlank(vararg strs: CharSequence?): Boolean {
        if (strs.isEmpty()) return true
        for (s in strs) {
            if (isNotBlank(s)) return false
        }
        return true
    }

    /**
     * 判断是否有空字符串
     *
     * @param str
     * @return
     */
    fun hasBlank(vararg strs: CharSequence?): Boolean {
        if (strs.isEmpty()) return true
        for (s in strs) {
            if (isBlank(s)) return true
        }
        return false
    }

    /**
     * 判断是否没有空字符串
     *
     * @param str
     * @return
     */
    fun hasNotBlank(vararg strs: CharSequence?): Boolean {
        if (strs.isEmpty()) return false
        for (s in strs) {
            if (isNotBlank(s)) return true
        }
        return false
    }

    fun startsWithIgnoreCase(str: String?, prefix: String?): Boolean {
        if (str == null && prefix == null) return true
        if (str == null || prefix == null) return false
        return str.uppercase().startsWith(prefix.uppercase())
    }

    fun endWithIgnoreCase(str: String?, suffix: String?): Boolean {
        if (str == null && suffix == null) return true
        if (str == null || suffix == null) return false
        return str.uppercase().endsWith(suffix.uppercase())
    }

}